﻿using System;
using System.Collections.Generic;
using System.Linq;
using MedRegService;

namespace MedRegSevice.Utils
{
    public class FlatEmployee
    {
        public Guid ID { get; set; }
        
        public string Name { get; set; }

        public string Surname { get; set; }

        public string Patromane { get; set; }

        public string Sex { get; set; }

        public int BearthYear { get; set; }

        public string AcademicDegree { get; set; }

        public string Address { get; set; }

        public string Post { get; set; }

        public string Speciality { get; set; }

        public string Category { get; set; }

        public decimal Experience { get; set; }

        public bool Fired { get; set; }

        public FlatEmployee (){}

        public static List<FlatEmployee> GetEmployeeList(Guid lpuID, string surname, int count)
        {
            var mdb = new MedregDBDataContext();

            var result = new List<FlatEmployee>();

            List<Employee> query = new List<Employee>();

            var absc = Math.Abs(count);
            
            if ( absc != 0)
            {
                query = mdb.Employee.Where(e => e.RealUZ == lpuID & e.Surname.ToUpper().StartsWith(surname)).Take(absc).ToList();
            }
            else
            {
                query = mdb.Employee.Where(e => e.RealUZ == lpuID & e.Surname.ToUpper().StartsWith(surname)).ToList();
            }

            foreach (Employee employee in query)
            {
                var flatemployee = new FlatEmployee();

                flatemployee.ID = employee.ID;
                flatemployee.Name = employee.Name;
                flatemployee.Patromane = employee.Patroname;
                flatemployee.Surname = employee.Surname;
                flatemployee.Sex = employee.Sex;
                flatemployee.BearthYear = employee.Birthdate.Year;
                flatemployee.AcademicDegree =
                    employee.PostGraduateEducation
                        .Where(pge => pge.Degree != null)
                        .OrderBy(pge => pge.DateEnd)
                        .Select(pge => pge.AcademicDegree.Name)
                        .LastOrDefault();
                flatemployee.Address = (from ae in employee.AddressEntity
                                        orderby ae.RegistrationDate
                                        select new
                                        {
                                            Address = ae.Territory.Name + ", "
                                                      + ae.KLADR.Name + ", "
                                                      + ae.KLADR1.Prefix + " "
                                                      + ae.KLADR1.Name + ", "
                                                      + ae.Street + ", "
                                                      + ae.House
                                                      + ae.Building + ", "
                                                      + ae.Apartment
                                        }).Last().Address;
                flatemployee.Post = employee.CardRecord
                    .Where(p => p.DateEnd == null)
                    .Select(p => p.Post.Name).LastOrDefault();

                flatemployee.Fired = (flatemployee.Post == null);

                flatemployee.Speciality = employee.DiplomaEducation
                    .Select(de => de.Speciality.Name).LastOrDefault();

                flatemployee.Category = employee.Qualification
                    .OrderBy(c => c.Year)
                    .Select(c => c.QualificationCategory.Name)
                    .LastOrDefault();

                flatemployee.Experience = ((decimal)(employee.CardRecord
                                .GroupBy(ex => ex.ID)
                                .Select(ex => ex.Sum(e => e.Duration))).Sum()) % 12;

                result.Add(flatemployee);
            }

            return result;
        }

        public static List<FlatEmployee> GetEmployeeList(Guid lpuID)
        {
            var mdb = new MedregDBDataContext();

            var result = new List<FlatEmployee>();

            foreach (Employee employee in mdb.Employee.Where(e => e.RealUZ == lpuID).ToList())
            {
                var flatemployee = new FlatEmployee();

                flatemployee.ID = employee.ID;
                flatemployee.Name = employee.Name;
                flatemployee.Patromane = employee.Patroname;
                flatemployee.Surname = employee.Surname;
                flatemployee.Sex = employee.Sex;
                flatemployee.BearthYear = employee.Birthdate.Year;
                flatemployee.AcademicDegree =
                    employee.PostGraduateEducation
                        .Where(pge => pge.Degree != null)
                        .OrderBy(pge => pge.DateEnd)
                        .Select(pge => pge.AcademicDegree.Name)
                        .LastOrDefault();
                flatemployee.Address = (from ae in employee.AddressEntity
                               orderby ae.RegistrationDate
                               select new
                                          {
                                              Address = ae.Territory.Name + ", "
                                                        + ae.KLADR.Name + ", "
                                                        + ae.KLADR1.Prefix + " "
                                                        + ae.KLADR1.Name + ", "
                                                        + ae.Street + ", "
                                                        + ae.House
                                                        + ae.Building + ", "
                                                        + ae.Apartment
                                          }).Last().Address;
                flatemployee.Post = employee.CardRecord
                    .Where(p => p.DateEnd == null)
                    .Select(p => p.Post.Name).LastOrDefault();
                
                flatemployee.Fired = (flatemployee.Post == null);
                
                flatemployee.Speciality = employee.DiplomaEducation
                    .Select(de => de.Speciality.Name).LastOrDefault();
                
                flatemployee.Category = employee.Qualification
                    .OrderBy(c => c.Year)
                    .Select(c => c.QualificationCategory.Name)
                    .LastOrDefault();

                flatemployee.Experience = ((decimal)(employee.CardRecord
                                .GroupBy(ex => ex.ID)
                                .Select(ex => ex.Sum(e => e.Duration))).Sum()) % 12 ;
                
                result.Add(flatemployee);
            }

            return result;
        }
    }
}